package com.yfjin_rtmp;

import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.projection.MediaProjection;
import android.os.Bundle;
import android.view.Surface;

import java.io.IOException;
import java.nio.ByteBuffer;

class VideoCodec extends Thread {

    private MediaProjection mediaProjection;
    private VirtualDisplay virtualDisplay;
    private ScreenLive screenLive;


    private MediaCodec mediaCodec;

    public VideoCodec(ScreenLive screenLive) {
        this.screenLive = screenLive;
    }


    public void stratLive(MediaProjection mediaProjection) {
        this.mediaProjection = mediaProjection;
        MediaFormat format = MediaFormat.createVideoFormat("video/avc", 720, 1280);
        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
        format.setInteger(MediaFormat.KEY_BIT_RATE, 400_000);
        format.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);


        try {
            mediaCodec = MediaCodec.createEncoderByType("video/avc");
            mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
            Surface surface = mediaCodec.createInputSurface();
            virtualDisplay = mediaProjection.createVirtualDisplay("screen-codec", 720, 1280, 1,
                    DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, surface, null, null
            );


        } catch (IOException e) {
            e.printStackTrace();
        }


        start();
    }


    private boolean isLiving = false;
    private long startTime;
    private long timeStamp;

    @Override
    public void run() {
        super.run();

        isLiving = true;
        mediaCodec.start();
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

        while (isLiving) {

            if (System.currentTimeMillis()-timeStamp>=2000){
                //每过2s钟触发I帧
                Bundle bundle=new Bundle();
                bundle.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME,0);
                mediaCodec.setParameters(bundle);
                timeStamp=System.currentTimeMillis();
            }


            int index = mediaCodec.dequeueOutputBuffer(info, 100 * 1000);
            if (index >= 0) {

                if (startTime==0){
                    startTime=info.presentationTimeUs/1000;
                }

                ByteBuffer buffer = mediaCodec.getOutputBuffer(index);
//                MediaFormat format = mediaCodec.getOutputFormat();
                byte[] outData = new byte[info.size];
                buffer.get(outData);

                RTMPPackage rtmpPackage=new RTMPPackage(outData,info.presentationTimeUs/1000-startTime,RTMPPackage.RTMP_PACKET_TYPE_VIDEO);
                screenLive.addPackage(rtmpPackage);
                mediaCodec.releaseOutputBuffer(index, false);
            }
        }


        isLiving = false;
        mediaCodec.stop();
        mediaCodec.release();
        mediaCodec = null;

        virtualDisplay.release();
        virtualDisplay = null;
        mediaProjection.stop();
        mediaProjection = null;
    }
}
